/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CelsiousConverterGUI.java
 *
 * Created on 27 січ 2009, 12:49:08
 */

package com.googlecode.bubbler.desktop;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.KeyStroke;

import com.googlecode.bubbler.api.WordDTO;
import com.googlecode.bubbler.api.WordNetFasade;
import com.googlecode.bubbler.api.WordNetFasadeImpl;

/**
 * 
 * @author MyName
 */
public class BubblerMainWindow extends javax.swing.JFrame {
    WordNetFasade wordFasade = new WordNetFasadeImpl();
    WordDTO wordDTO = null;

    /** Creates new form CelsiousConverterGUI */
    public BubblerMainWindow() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        in = new javax.swing.JTextField();
        scrollPanel = new javax.swing.JScrollPane();
        out = new javax.swing.JTextArea();
        button = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("bubbler "); // NOI18N
        addkeyBind();
        in.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                inKeyPressed(evt);
            }
        });

        out.setColumns(20);
        out.setRows(5);
        scrollPanel.setViewportView(out);

        button.setText("search");
        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/18_US.jpg"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrollPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
                    .addComponent(in, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(button, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(in, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(button)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
        if(wordDTO!=null && wordDTO.isPronunciationExists()){
            wordDTO.getPronunciation().play();
        }
            
        //out.setText("loud");
    }// GEN-LAST:event_jButton1ActionPerformed

    private void buttonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_buttonActionPerformed
        // evt.
        // out.setText(in.getText());
         searchWord();
        // TODO add your handling code here:
    }// GEN-LAST:event_buttonActionPerformed

    private void searchWord() {
        wordDTO = wordFasade.getWordDTO(in.getText());
        out.setText(wordDTO.getSentens());
    }

    private void inKeyPressed(java.awt.event.KeyEvent evt) {// GEN-FIRST:event_inKeyPressed
       // searchWord();
        // out.setText(in.getText());
    }// GEN-LAST:event_inKeyPressed
    
    private void addkeyBind(){
        in.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "doSomething");
        in.getActionMap().put("doSomething", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                searchWord();
            }
        });
    }

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BubblerMainWindow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button;
    private javax.swing.JTextField in;
    private javax.swing.JButton jButton1;
    private javax.swing.JTextArea out;
    private javax.swing.JScrollPane scrollPanel;
    // End of variables declaration//GEN-END:variables

}
